package com.hinotoyk.testsystem.service;

import com.hinotoyk.testsystem.entity.*;
import com.hinotoyk.testsystem.mappers.StuAnswersInfoMapper;
import com.hinotoyk.testsystem.mappers.StudentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: testsystem
 * @description:
 * @author: DYJ
 * @create: 2019-11-16 13:17
 **/
@Service
public class StudentService {
    @Resource
    private StudentMapper studentMapper;

    @Resource
    private StuAnswersInfoMapper stuAnswersInfoMapper;

    @Resource
    private TestPaperService testPaperService;



    public int register(Student student){
        return studentMapper.insert(student);
    }

    public Student getStudent(String stuName){
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andStudentNameEqualTo(stuName);
        List<Student> students = studentMapper.selectByExample(studentExample);
        return students.get(0);
    }
    public Student getStudentById(int studentId){
        return studentMapper.selectByPrimaryKey(studentId);
    }

    public List<TestPaperInfo> getAllTests(int studentId, int status){
        StuAnswersInfoExample example = new StuAnswersInfoExample();
        StuAnswersInfoExample.Criteria criteria = example.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        criteria.andStatusEqualTo(status);
        List<StuAnswersInfo> Sai = stuAnswersInfoMapper.selectByExample(example);
        List<TestPaperInfo> list = new ArrayList<>();
        for(int i=0;i<Sai.size();i++){
            Integer testPaperId = Sai.get(i).getTestPaperId();
            TestPaperInfo tpi = testPaperService.FindTestPaper(testPaperId);
            list.add(tpi);
        }
        return list;
    }

    public List<Student> getAllStudent(){
        return studentMapper.selectByExample(new StudentExample());
    }

}
    